<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="utf-8">
    <title>登入</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="fly,layui,前端社区">
    <meta name="description" content="Fly社区是模块化前端UI框架Layui的官网社区，致力于为web开发提供强劲动力">
    <link rel="stylesheet" th:href="@{/res/layui/css/layui.css}">
    <link rel="stylesheet" th:href="@{/res/css/global.css}">
</head>
<body>
<!-- 引入header -->
<div th:replace="common/header::bbsHeader"></div>
<div class="layui-container fly-marginTop">
    <div class="fly-panel fly-panel-user" pad20>
        <div class="layui-tab layui-tab-brief" lay-filter="user">
            <ul class="layui-tab-title">
                <li class="layui-this">登入</li>
            </ul>
            <div class="layui-form layui-tab-content" id="LAY_ucm" style="padding: 20px 0;">
                <div class="layui-tab-item layui-show">
                    <div class="layui-form layui-form-pane">
                        <form id="loginForm" method="post">
                            <div class="layui-form-item">
                                <label for="username" class="layui-form-label">用户名</label>
                                <div class="layui-input-inline">
                                    <input type="text" id="username" name="username" required lay-verify="required"
                                           autocomplete="off" class="layui-input" placeholder="请输入用户名">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label for="password" class="layui-form-label">密码</label>
                                <div class="layui-input-inline">
                                    <input type="password" id="password" name="password" required lay-verify="required"
                                           autocomplete="off" class="layui-input" placeholder="请输入密码">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <button class="layui-btn" lay-filter="login" lay-submit>立即登录</button>
                                <input type="checkbox" name="remember-me" title="记住我" checked>
                                <span style="padding-left:20px;">
                                    <a href="forget.html">忘记密码？</a>
                                </span>
                            </div>
                            <div class="layui-form-item">
                                <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 引入footer -->
<div th:replace="common/footer::bbsFooter"></div>
<script th:src="@{/res/layui/layui.js}"></script>
<script type="text/javascript">
    layui.use(['form', 'layer','jquery'], function () {
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;

        //监听提交
        form.on('submit(login)', function (data) {
            $.ajax({
                type: "POST",
                url: "/login",
                data: $('#loginForm').serialize(),
                dataType: "json",
                success: function (loginCallback) {
                    if (loginCallback.code == '200') {
                        layer.msg("登录成功", {
                            icon: 6,
                            time: 1000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {    //弹框后的操作
                            window.location.href = "/";
                        });
                    } else {
                        layer.msg("用户名或密码错误，请重新输入", {
                            icon: 2,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        });
                    }
                },
                error: function (jqXHR) {
                    layer.alert("发生错误：" + jqXHR.status, {
                        title: 'Error'
                    });
                }
            });
            return false;
        });
    })
</script>
<script>
    layui.cache.page = '';
    layui.cache.user = {
        username: '游客'
        , uid: -1
        , avatar: '../res/images/avatar/00.jpg'
        , experience: 83
        , sex: '男'
    };
    layui.config({
        version: "3.0.0"
        , base: '../res/mods/' //这里实际使用时，建议改成绝对路径
    }).extend({
        fly: 'index'
    }).use('fly');
</script>
</body>
</html>